package com.example.springboot.controller;


import com.example.springboot.mapper.ScoreMapper;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.common.Result;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot.service.IScoreService;
import com.example.springboot.entity.Score;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/score")
    public class ScoreController {
    
    @Resource
    private IScoreService scoreService;

    @Resource
    private ScoreMapper scoreMapper;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Score score) {
        score.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return scoreService.insertScore(score);
    }

    //查找所有
    @GetMapping
    public Result findAll() {
        return Result.success(scoreService.list());
    }

    //查找当前用户对浏览方案的打分情况
    @GetMapping("/getBySolution")
    public Result getBySolution(@RequestParam Integer solutionId) {
        return Result.success(scoreService.getScoreByUserAndSolution(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId(),solutionId));
    }

    //查找方案的分数和打分人数
    @GetMapping("/getScoreAndCount")
    public Result getScoreAndCount(@RequestParam Integer solutionId) {
        HashMap<String,Double> hashMap = new HashMap<>();
        Double score = scoreService.getScore(solutionId);
        if (score == null)
            return Result.success(null);
        hashMap.put("score",score);
        hashMap.put("count",scoreService.getCount(solutionId).doubleValue());
        return Result.success(hashMap);
    }
}

